package cn.seaboot.excel.sax;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;

/**
 * parse tag: table
 *
 * @author Mr.css
 * @version 2025-07-01 14:37
 */
public class TableParser implements DocumentParser {
    /**
     *
     */
    private final TableColumnParser columnParser = new TableColumnParser();
    /**
     * thead 解析器可以解析所有内容
     */
    private final TableHeadParser parser = new TableHeadParser();
    /**
     * 数据行解析器
     */
    private DocumentParser tableRowParser = parser.getTableRowParser();
    /**
     * 表头解析器
     */
    private DocumentParser tableHeadParser = parser;
    /**
     * 表尾解析器
     */
    private DocumentParser tableFootParser = parser;
    /**
     * 内容解析器
     */
    private DocumentParser tableBodyParser = parser;

    public DocumentParser getTableRowParser() {
        return tableRowParser;
    }

    public void setTableRowParser(DocumentParser tableRowParser) {
        this.tableRowParser = tableRowParser;
    }

    public DocumentParser getTableHeadParser() {
        return tableHeadParser;
    }

    public void setTableHeadParser(DocumentParser tableHeadParser) {
        this.tableHeadParser = tableHeadParser;
    }

    public DocumentParser getTableFootParser() {
        return tableFootParser;
    }

    public void setTableFootParser(DocumentParser tableFootParser) {
        this.tableFootParser = tableFootParser;
    }

    public DocumentParser getTableBodyParser() {
        return tableBodyParser;
    }

    public void setTableBodyParser(DocumentParser tableBodyParser) {
        this.tableBodyParser = tableBodyParser;
    }

    /**
     * 读取 xml 模版输入流
     *
     * @param is 输入流
     * @throws DocumentException - sax 解析异常
     */
    public void parse(InputStream is) throws DocumentException, IOException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(is);

        Element table = document.getRootElement();
        parse(table);
    }

    /**
     * 读取 table 内容
     *
     * @param table 表格
     */
    @Override
    public void parse(Element table) throws IOException {
        for (Element ele : table.elements()) {
            String name = ele.getName();
            if ("tr".equals(name)) {
                tableRowParser.parse(ele);
            } else if("colgroup".equals(name)) {
                columnParser.parse(ele);
            } else if ("thead".equals(name)) {
                tableHeadParser.parse(ele);
            } else if ("tbody".equals(name)) {
                tableBodyParser.parse(ele);
            } else if ("tfoot".equals(name)) {
                tableFootParser.parse(ele);
            } else {
                throw new IllegalArgumentException("illegal element: " + name);
            }
        }
    }

    @Override
    public void setContext(ExcelContext context) {
        columnParser.setContext(context);
        tableRowParser.setContext(context);
        tableHeadParser.setContext(context);
        tableFootParser.setContext(context);
        tableBodyParser.setContext(context);
    }
}
